<?php

class Mail
{

    //public $_useSMTP = true;
    public $_useSMTP = false;
    public $subject;       // (string) ����
    public $text;          // (string) ����� ��������� (txt-�������)
    public $html;          // (string) ����� ��������� (html-�������)
    public $from;          // (string) �� ����
    public $to;            // (string) ����
    public $copy;   // (string) Copy to
    public $bcc;
    public $charset;       // (string) ��������� (�� ��������� Windows-1251)
    public $sHeaders;       // (string)
    public $sBody;          // (string)
    public $sContentType;   // (string)
    public $sHtmlTemplate;  // (string)
    public $sBoundary;      // (string)
    public $aAttaches;      // (array)

    public function __construct()
    {
        $this->charset = 'UTF-8';
        $this->aAttaches = array();
        $this->sBoundary = '----' . substr(md5(uniqid(rand(), true)), 0, 16);
        $this->sHtmlTemplate = '<html><head><title>{title}</title></head><body>{body}</body></html>';
    }

    function Attach($sPath, $mimeType=null)
    {
        if (file_exists($sPath))
        {
            $sName = basename($sPath);


            if (!$mimeType)
            {
                $mimeType = '';
                /*
                  image/bmp                            bmp
                  image/gif                            gif
                  image/jpeg                           jpeg jpg jpe
                  image/png                            png
                 */
                $ext = explode('.', $sPath);
                $ext = strtolower(trim(array_pop($ext)));
                
                switch ($ext)
                {
                    case('pdf'):$mimeType.='application/pdf';
                        break;
                    case('docx'):case('doc'):$mimeType.='application/msword';
                        break;
                    
                    case('bmp'):$mimeType.='image/bmp';
                        break;
                    case('gif'):$mimeType.='image/gif';
                        break;
                    case('png'):$mimeType.='image/png';
                        break;
                    case('jpg'):case('jpe'):case('jpeg'):$mimeType.='image/jpeg';
                        break;
                    default: return '0'; //throw new Exception(500, 'Wrong extension of file!');
                }
            }

            $sAttach = "Content-Type: $mimeType; name=\"$sName\"\r\n";
            $sAttach.="Content-Disposition: attachment; filename=\"$sName\"\r\n";
            $sAttach.="Content-Transfer-Encoding: base64\r\n";
            $sAttach.="\r\n";
            $sAttach.=base64_encode(file_get_contents($sPath)) . "\r\n";
            $this->aAttaches[] = $sAttach;
            return '1';
        }
    }

    public function Send($log=false)
    {
        $mailAlwaysBcc = '';

        $this->sHeaders = "From: {$this->from}\r\n";
        //$log=false;
        $recipients = array();

        if (!function_exists('_AddRec'))
        {

            function _AddRec(&$rec, $add)
            {
                if (!$add || $add == '')
                    return;
                $cc = explode(',', $add);
                if (!is_array($cc)

                    )$cc = array($cc);
                $rec = array_merge($cc, $rec);
            }

        }
        _AddRec($recipients, $this->to);

        if ($this->copy != '')
        {
            $this->sHeaders .="Cc: {$this->copy}\r\n";

            _AddRec($recipients, $this->copy);
        }


        if ($this->bcc != '' && $mailAlwaysBcc != '')
            $this->sHeaders .="bcc: {$this->bcc}, {$mailAlwaysBcc}\r\n";
        else
        if ($this->bcc != '' && $mailAlwaysBcc == '')
            $this->sHeaders .="bcc: {$this->bcc}\r\n";
        else
        if ($this->bcc == '' && $mailAlwaysBcc != '')
            $this->sHeaders .="bcc: {$mailAlwaysBcc}\r\n";

        _AddRec($recipients, $this->bcc);
        _AddRec($recipients, $mailAlwaysBcc);

        $this->sHeaders.="MIME-Version: 1.0\r\n";

        $aFields = array();
        $aFields['{title}'] = $this->subject;
        $aFields['{body}'] = $this->html;


        if (count($this->aAttaches) > 0)
        {
            $this->sHeaders.="Content-Type: multipart/mixed; boundary=\"{$this->sBoundary}\"\r\n";
            $this->sBody .= "--{$this->sBoundary}\r\n";
            $this->sBody .= "Content-Type: text/html; charset={$this->charset}\r\n";
            $this->sBody .= "Content-Transfer-Encoding: 8bit\r\n";
            $this->sBody .= "\r\n";

            $this->sBody .= strtr($this->sHtmlTemplate, $aFields);
            $this->sBody .= "\r\n";
            foreach ($this->aAttaches as $sAttach)
            {
                $this->sBody .= "--{$this->sBoundary}\r\n";
                $this->sBody .= $sAttach;
            }
            $this->sBody .= "--{$this->sBoundary}--\r\n";
        }
        else
        {
            $this->sHeaders.='Content-Type: text/html; charset=' . $this->charset . "\r\n";
            $this->sBody = strtr($this->sHtmlTemplate, $aFields);
        }

        $this->sHeaders.="Subject: {$this->subject}\r\n";
        $this->sHeaders.="To: {$this->to}\r\n";
        $this->sHeaders.="From: {$this->from}\r\n";

        return @mail($this->to, $this->subject, $this->sBody, $this->sHeaders);
    }

}

?>